package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.Dto.DishDto;
import com.example.demo.Dto.SetmealDto;
import com.example.demo.common.R;
import com.example.demo.common.pageResult;
import com.example.demo.entiy.Setmeal;
import com.example.demo.mapper.SetmealMapper;
import com.example.demo.service.SetmeaiServiceImp;
import com.example.demo.service.SetmealDishServiceImp;
import com.example.demo.service.ipml.SetmealDishService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmeaiServiceImp setmeaiServiceImp;
    @GetMapping("/page")
    public R<Object> getPage(int page,int pageSize,String name){
        pageResult<SetmealDto> meal  =setmeaiServiceImp.getPage(page,pageSize,name);
        return R.success(JSON.toJSON(meal));
    }
    @PostMapping
    public R<String> PutInfo(HttpServletRequest request, @RequestBody SetmealDto setmealDto){
        Long empId = (Long)request.getSession().getAttribute("employee");
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        boolean inser = setmeaiServiceImp.PutInfo(setmealDto);

        return R.success("添加成功");
    }
    @PostMapping("/status/{status}")
    public R<String> ChangeStatus(Long[] ids,@PathVariable int status){
        if (setmeaiServiceImp.changeStatus(status, Arrays.asList(ids))){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @DeleteMapping
    public R<String> deleteDis(Long[] ids){
        if (setmeaiServiceImp.deletedis(Arrays.asList(ids))){
            return R.success("删除成功！");
        }
        return R.error("删除失败");
    }
    @GetMapping("/{id}")
    public R<SetmealDto> getinfo(@PathVariable Long id){
        SetmealDto setmealDto = setmeaiServiceImp.getInfo(id);
        return R.success(setmealDto);
    }
    @PutMapping
    public R<String> putDishinfo(HttpServletRequest request, @RequestBody SetmealDto setmealDto){
        Long empId = (Long) request.getSession().getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealDto.setUpdateTime(LocalDateTime.now());


        if (setmeaiServiceImp.UpdataInfo(setmealDto)!=0){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
}
